package servlet;

import util.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookieServlet extends BaseServlet {

    /**
     * 服务器修改Cookie
     **/
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        /*方案一*/
        /*//1.先创建一个要修改的同名的Cookie对象
        //2.在构造器，同时赋予新的Cookie
        Cookie cookie = new Cookie("key1", "newValue1");
        //3.调用response.addCookie( Cookie ); 通知 客户端 保存修改
        resp.addCookie(cookie);
        resp.getWriter().write(cookie.getName() + "的Cookie已经修改好了");*/

        /*方案二*/
        //1.先查找到需要修改的Cookie对象
        Cookie cookie = CookieUtils.findCookie("key2", req.getCookies());
        //2.调用setValue()方法赋予新的Cookie值
        if (cookie != null) {
            cookie.setValue("newValue2");
        }
        //3.调用response.addCookie()通知客户端保存修改
        resp.addCookie(cookie);
        resp.getWriter().write(cookie.getName() + "的Cookie已经修改好了");
    }

    /**
     * 服务器获取Cookie
     **/
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        //返回客户端Cookie对象
        for (Cookie cookie : cookies) {
            //getName方法返回Cookie的key(名)
            //getValue方法返回Cookie的value值
            resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "]<br/>");
        }

        //获取需要的Cookie
        Cookie iWantCookie = CookieUtils.findCookie("key1", cookies);
        /*for (Cookie cookie : cookies) {
            if ("key2".equals(cookie.getName())) {
                iWantCookie = cookie;
                break;
            }
        }*/
        //如果不等于null，说明赋过值，也就是找到了需要的Cookie
        if (iWantCookie != null) {
            resp.getWriter().write("找到了需要的Cookie");
        }
    }

    /**
     * 创建Cookie
     **/
    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie cookie1 = new Cookie("key1", "value1");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie1);

        //1.创建Cookie对象
        Cookie cookie2 = new Cookie("key2", "value2");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie2);
        //Cookie可以一次创建多个
        resp.getWriter().write("Cookie创建成功");
    }

    /**
     * 默认的会话级别的Cookie
     **/
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Cookie cookie = new Cookie("defaultLife","defaultLife");
        cookie.setMaxAge(-1);//设置存活时间  正数表示存活时间，零表示立即删除，负数表示浏览器关闭删除Cookie
        resp.addCookie(cookie);
    }

    /**
     * 马上删除一个Cookie
     **/
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Cookie cookieNow = new Cookie("keyNow", "valueNow");
        resp.addCookie(cookieNow);
        // 先找到你要删除的Cookie对象
        Cookie cookie = CookieUtils.findCookie("keyNow", req.getCookies());
        if (cookie != null) {
            // 调用setMaxAge(0);
            cookie.setMaxAge(0); // 表示马上删除，都不需要等待浏览器关闭
            // 调用response.addCookie(cookie);
            resp.addCookie(cookie);
            resp.getWriter().write("keyNow的Cookie已经被删除");
        }
    }

    /**
     * 设置存活1个小时的Cookie
     **/
    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600", "life3600");
        cookie.setMaxAge(60 * 60); // 设置Cookie一小时之后被删除。无效
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了一个存活一小时的Cookie");
    }

    /**
     * Cookie有效路径设置
     **/
    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path1", "path1");
        // getContextPath() ===>>>>  得到工程路径
        cookie.setPath( req.getContextPath() + "/abc" ); // ===>>>>  /工程路径/abc
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有Path路径的Cookie");
    }
}
